package com.example.popmp3.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.popmp3.dao.MusicListDAO;
import com.example.popmp3.dao.SingleListDAO;
import com.example.popmp3.model.Tb_musiclist;
import com.example.popmp3.activity.DemoAdapter.ViewHolder;
import com.example.popmp3.activity.DemoAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.popmp3.R;
import com.example.popmp3.model.Tb_singlelist;

/**
 * Created by Administrator on 2016/3/9.
 */
public class InMusic extends Activity implements OnClickListener,OnItemClickListener {
    //public static final String FLAG = "id";// 定义一个常量，用来作为请求码
    String loveid;

    MusicListDAO musicListDAO = new MusicListDAO(InMusic.this);
    SingleListDAO singleListDAO=new SingleListDAO(InMusic.this);

    String[] strInfos = null;
    private ViewGroup btnCancle = null;
    private ViewGroup btnAdd = null;
    private Button btnSelectAll = null;
    private Button btnDelete = null;
    private ListView lvListView = null;
    private DemoAdapter adpAdapter = null;
    private List<DemoBean> datas = null;
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inmusic);

        btnCancle = (ViewGroup) findViewById(R.id.btnCancle);
        btnAdd = (ViewGroup) findViewById(R.id.btnAdd);
        btnSelectAll = (Button) findViewById(R.id.btnSelectAll);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        lvListView = (ListView) findViewById(R.id.lvListView); // 获取布局管理器中添加的ListView组件

        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取便签id
        loveid = bundle.getString("id");// 将便签id转换为字符串

        ShowInfo();

        btnCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<Integer, Boolean> map = adpAdapter.getCheckMap();
                // 进行遍历
                for (int i = 0; i < musicListDAO.getMaxId(); i++) {
                    if (map.get(i) != null && map.get(i)) {
                        Tb_singlelist tb_singlelist = new Tb_singlelist(singleListDAO.getMaxId() + 1,
                                musicListDAO.find(i + 1).getid(),Integer.parseInt(loveid));
                        label:
                        if (singleListDAO.getMaxId()>=1){
                            for (int j= 0;j<singleListDAO.getMaxId();j++) {
                                if (musicListDAO.find(i+1).getName().equals(
                                        musicListDAO.find(singleListDAO.find(j + 1).getMusicid()).getName())
                                        &&singleListDAO.find(j + 1).getLoveid()==Integer.parseInt(loveid)) {
                                    Toast.makeText(InMusic.this,musicListDAO.find(i+1).getName()+"已经存在！",Toast.LENGTH_SHORT).show();
                                    break label;
                                }
                            }
                            singleListDAO.add(tb_singlelist);
                        } else {
                            singleListDAO.add(tb_singlelist);
                        }
                    }
                }
                //adpAdapter.notifyDataSetChanged();
                finish();
            }
        });

        btnSelectAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnSelectAll.getText().toString().trim().equals("全选")) {
                    // 所有项目全部选中
                    adpAdapter.configCheckMap(true);
                    adpAdapter.notifyDataSetChanged();
                    btnSelectAll.setText("全不选");
                } else {
                    // 所有项目全部不选中
                    adpAdapter.configCheckMap(false);

                    adpAdapter.notifyDataSetChanged();

                    btnSelectAll.setText("全选");
                }
            }
        });
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Map<Integer, Boolean> map = adpAdapter.getCheckMap();
                // 获取当前的数据数量
                //int count = adpAdapter.getCount();
                // 进行遍历
                for (int i = 0; i < musicListDAO.getMaxId(); i++) {
                    if (map.get(i) != null && map.get(i)) {
                        musicListDAO.detele(i + 1);
                    }
                }
                adpAdapter.notifyDataSetChanged();
            }
        });
        lvListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View itemLayout, int position, long id) {
                if (itemLayout.getTag() instanceof ViewHolder) {
                    ViewHolder holder = (ViewHolder) itemLayout.getTag();
                    // 会自动出发CheckBox的checked事件
                    holder.cbCheck.toggle();
                }

            }
        });
    }
    public void ShowInfo() {
        List<Tb_musiclist> musiclists = musicListDAO.getScrollData(0,
                (int) musicListDAO.getCount());
        strInfos = new String[musiclists.size()];// 设置字符串数组的长度
        int m = 0;// 定义一个开始标识
        List<DemoBean> demoDatas = new ArrayList<DemoBean>();
        for (Tb_musiclist tb_musiclist : musiclists) {// 遍历List泛型集合
            strInfos[m] = tb_musiclist.getid() + " " + tb_musiclist.getName();
            demoDatas.add(new DemoBean(strInfos[m].toString(), true));
            m++;// 标识加1
        }
        adpAdapter = new DemoAdapter(this, demoDatas);
        lvListView.setAdapter(adpAdapter);
    }

    @Override
    protected void onRestart () {
        super.onRestart();// 实现基类中的方法
        ShowInfo();// 显示收入信息
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}